//# sourceURL=http://uMapApp/Module/Bridge/BridgeProfileModule
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../Module",
    "../../UI/Panel/FloatPanel",
    "../../UI/Panel/PanelBlock/PanelBlock",
    "../../UI/Panel/PanelBlock/ListBlock",
    "../../UI/Panel/PanelBlock/ChartBlock",
    "../../UI/Panel/PanelBlock/PropertiesBlock",
    "../../UI/Panel/PanelBlock/TreeBlock",
    "../../UI/Tree/Tree",
    "../../UI/Panel/Item/ListItem"
    , "./BridgeProfile"
    , "./BridgeFiles"
    , "./BridgeFile"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel,
    PanelBlock,
    ListBlock,
    ChartBlock,
    PropertiesBlock,
    TreeBlock,
    Tree,
    ListItem,
    BridgeProfile,
    BridgeFiles,
    BridgeFile
) {

    function BridgeProfileModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "桥梁档案";
        _this.icon = _this.app.icons.bridge;
        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showMinMax": true, "title": "桥梁列表"});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock", "title": "桥梁"});

        _this.listPanel.listBlock.filter = {
            "param": "bridgeProfile.searchKey"
        };

        _this.listPanel.listBlock.sorter = {
            "param": {
                "value": "bridgeProfile.sortKey",
                "label": "桩号"
            }
        };

        _this.listPanel
            .append(_this.listPanel.listBlock);

        _this.panels.push(_this.listPanel);


        _this.filePanel = new FloatPanel({"class": "leftPanel", "showMinMax": true, "title": "档案列表"});
        _this.filePanel.treeBlock = new TreeBlock({"class": "fullBlock"});
        _this.filePanel.append(_this.filePanel.treeBlock);
        _this.filePanel.treeBlock.title = "档案列表";

        _this.filePanel.treeBlock.tree.root = _this.filePanel.treeBlock.tree.add(new Tree({
            title: "G50",
            path : "/"
        }));

        _this.filePanel.treeBlock.tree.bridgeTree = _this.filePanel.treeBlock.tree.add(new Tree({
            title: "G50",
            list : []
        }));

        _this.filePanel.treeBlock.tree.bridgeTree.openChild();
        _this.filePanel.treeBlock.tree.bridgeTree.hide();

        _this.panels.push(_this.filePanel);

        // _this.listPanel.listBlock.filter = {
        //     "param": "bridgeProfile.searchKey"
        // };
        //
        // _this.listPanel.listBlock.sorter = {
        //     "param": {
        //         "value": "bridgeProfile.sortKey",
        //         "label": "桩号"
        //     }
        // };

        _this.listPanel
            .append(_this.listPanel.listBlock);

        _this.panels.push(_this.listPanel);

        // _this.detailPanel = new FloatPanel({"class": "leftPanel", "showClose": true});
        // _this.detailPanel.propertiesBlock = new PropertiesBlock({"class": "fullBlock"});
        //
        // _this.detailPanel
        //     .append(_this.detailPanel.propertiesBlock);
        //
        // _this.detailPanel.on("close", function () {
        //     _this.trigger("bridgeUnSelect");
        // });
        //
        // _this.panels.push(_this.detailPanel);

        _this.elementListPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.elementListPanel.treeBlock = new TreeBlock({"class": "fullBlock"});

        _this.elementListPanel
            .append(_this.elementListPanel.treeBlock);

        _this.elementListPanel.on("close", function () {
            _this.trigger("bridgeUnSelect");
        });

        _this.panels.push(_this.elementListPanel);

        _this.elementDetailPanel = new FloatPanel({"class": "rightPanel", "showClose": true});
        _this.elementDetailPanel.treeBlock = new TreeBlock({"class": "fullBlock", "title": "桥梁构件档案"});

        _this.elementDetailPanel
            .append(_this.elementDetailPanel.treeBlock);

        _this.elementDetailPanel.on("close", function () {
            _this.trigger("bridgeElementUnSelect");
        });

        _this.panels.push(_this.elementDetailPanel);

        _this.on("bridgeSelect", function (bridgeProfile) {

            _this.activeItem(bridgeProfile);

            _this.loadBridgeElements(bridgeProfile, function (list) {

                _this.listPanel.close(false);

                _this.elementListPanel.treeBlock.title = bridgeProfile.title;

                _this.elementListPanel.treeBlock.tree.clear();
                _this.elementListPanel.treeBlock.tree.add({
                    "title": "桥梁构件",
                    "list" : _this.renderElementsTree(list)
                });

                if (bridgeProfile.extendProperties) {
                    $.each(bridgeProfile.extendProperties, function (context, catalog) {

                        var tree = _this.elementListPanel.treeBlock.tree.add({
                            "title": context,
                            "list" : []
                        });

                        $.each(catalog, function (context, value) {
                            tree.add({
                                "title": context + " : " + value
                            });
                        });

                    });
                }

                _this.elementListPanel.open(false);

            });

            _this.toggleBrideTree(bridgeProfile);

        });

        _this.on("bridgeUnSelect", function () {
            _this.unActiveAllItem();
            _this.toggleBrideTree();
            _this.listPanel.open(false);
            _this.elementListPanel.close(false);
            _this.elementDetailPanel.close(false);
        });

        _this.on("bridgeElementSelect", function (elementNum) {

            _this.openElementFiles(elementNum, function (success) {
                // if (success) {
                _this.elementListPanel.close(false);
                _this.elementDetailPanel.open(false);
                // }
            });
        });

        _this.on("bridgeElementUnSelect", function () {
            _this.elementListPanel.open(false);
            _this.elementDetailPanel.close(false);
        });

        _this.app.ui.layout.right.append(_this.listPanel);
        _this.app.ui.layout.left.append(_this.filePanel);
        _this.app.ui.layout.right.append(_this.elementListPanel);
        _this.app.ui.layout.right.append(_this.elementDetailPanel);

        // _this.bridges = [];
        //
        // _this.app.servers.DC.requests.bridge.send({}, function (json) {
        //
        //     $.each(json.data.list, function (i, item) {
        //         item.app = _this.app;
        //         var bridge = new Bridge(item);
        //         var listItem = new ListItem({
        //             "title"    : bridge.name,
        //             "subtitle" : bridge.road.name,
        //             "icon"     : _this.app.icons.bridge.base64
        //         });
        //
        //         bridge.on("elementSelect", function (elementNum) {
        //             _this.elementListPanel.close();
        //             _this.openElementFiles(elementNum);
        //         });
        //
        //         listItem.on("click", function () {
        //
        //             bridge.layer.active();
        //             _this.chartPanel.close();
        //             _this.listPanel.close();
        //
        //             _this.app.servers.DC.requests.bridge_map.send({}, function (bridge_propertise_map_json) {
        //                 _this.detailPanel.propertiesBlock.title = bridge.name;
        //                 _this.detailPanel.propertiesBlock.map = bridge_propertise_map_json.data;
        //                 _this.app.servers.DC.requests.bridge_properties.send({"id":bridge.id}, function (bridge_propertise_json) {
        //                     _this.detailPanel.propertiesBlock.properties = bridge_propertise_json.data;
        //                     _this.detailPanel.open();
        //                 })
        //             });
        //
        //
        // _this.app.servers.DC.requests.bridge_node.send({"bridgeId": bridge.id}, function (json) {
        //
        //     function getTree(array) {
        //
        //         var list = [];
        //
        //         $.each(array, function (i, item) {
        //             if (item.children && item.children.length > 0) {
        //                 list.push({
        //                     "title": item.title,
        //                     "list": getTree(item.children)
        //                 });
        //             } else {
        //
        //                 var tree = new Tree({
        //                     "title": item.title
        //                 });
        //
        //                 tree.ui.titleDiv.on("click", function () {
        //
        //                     if (item.bridgeFilesId) {
        //                         _this.elementListPanel.close();
        //                         _this.openElementFiles(item.num);
        //                     }
        //
        //
        //                 });
        //
        //                 list.push(tree);
        //             }
        //         });
        //
        //         return list;
        //     }
        //
        //     _this.elementListPanel.treeBlock.tree.clear();
        //
        //     _this.elementListPanel.treeBlock.list = [
        //         {
        //             "title": bridge.name,
        //             "list": getTree(json.data.list)
        //         }
        //     ];
        //
        //     _this.elementListPanel.open();
        //
        // });
        //
        //         });
        //
        //         _this.listPanel.listBlock.add(listItem);
        //
        //     });
        //
        //
        // });
    }

    var _class = BridgeProfileModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "loadBridgeFileTree": {
            value: function (bridge, callback) {

                var _this = this;

                if (_this.bridgeFileRequest) {
                    _this.bridgeFileRequest.send({bridgeName: bridge.title}, function (json) {
                        callback && callback(json.data);
                    });
                }

            }
        },
        "toggleBrideTree"   : {
            value: function (bridge) {

                var _this = this;

                if (bridge) {

                    _this.filePanel.treeBlock.tree.bridgeTree.show();
                    _this.filePanel.treeBlock.tree.root.hide();
                    $.each(_this.filePanel.treeBlock.tree.bridgeTree.list, function (i, tree) {
                        if (tree.bridge === bridge) {
                            tree.show();
                            _this.openBridgeFileTree(bridge);
                        } else {
                            tree.hide();
                        }
                    });

                } else {
                    _this.filePanel.treeBlock.tree.bridgeTree.hide();
                    _this.filePanel.treeBlock.tree.root.show();
                }


            }
        },
        "openBridgeFileTree": {
            value: function (bridge) {

                var _this = this;

                var tree = _this.filePanel.treeBlock.tree.bridgeTree[bridge.id];

                if (!tree.isChildLoaded) {

                    tree.ui.icon.addClass("loading");

                    _this.loadBridgeFileTree(bridge, function (list) {

                        tree.ui.icon.removeClass("loading");
                        tree.isChildLoaded = true;

                        $.each(list, function (i, item) {


                            if (item.children) {
                                //目录

                                var childTree = new Tree({
                                    "title": item.name
                                });

                                $.each(item.children, function (j, itemChild) {

                                    itemChild.fileFormat = _this.app.servers.DC.fileFormat.bridgeFile;
                                    itemChild.modalityWindow = _this.app.modalityWindow;

                                    var bridgeFiles = new BridgeFiles(itemChild);

                                    var bridge_files_tree = new Tree({
                                        "title": bridgeFiles.title
                                    });

                                    bridge_files_tree.ui.titleDiv.on("click", function () {
                                        bridgeFiles.openAttachment();
                                    });

                                    childTree.add(bridge_files_tree);

                                });

                                tree.add(childTree);

                            } else {
                                //文件
                                item.fileFormat = _this.app.servers.DC.fileFormat.bridgeFile;
                                item.modalityWindow = _this.app.modalityWindow;

                                var bridgeFiles = new BridgeFiles(item);

                                var bridge_files_tree = new Tree({
                                    "title": bridgeFiles.title
                                });

                                bridge_files_tree.ui.titleDiv.on("click", function () {
                                    bridgeFiles.openAttachment();
                                });

                                tree.add(bridge_files_tree);
                            }

                        });

                        tree.openChild();

                    });
                }

            }
        },
        "loadFileTree"      : {
            value: function (path, callback) {

                var _this = this;

                if (_this.fileTreeRequest) {
                    _this.fileTreeRequest.send({path: path}, function (json) {
                        callback && callback(json.data);
                    });
                }

            }
        },
        "setFileTree"       : {
            value: function (tree) {

                var _this = this;

                if (!tree.isChildLoaded) {

                    tree.ui.icon.addClass("loading");

                    _this.loadFileTree(tree.path, function (list) {

                        tree.ui.icon.removeClass("loading");
                        tree.isChildLoaded = true;

                        $.each(list, function (i, item) {


                            if (item.fileUrl && item.fileUrl.search(".pdf") >= 0) {

                                //文件
                                item.fileFormat = _this.app.servers.DC.fileFormat.bridgeFile;
                                item.modalityWindow = _this.app.modalityWindow;

                                var bridgeFiles = new BridgeFiles(item);


                                var bridge_files_tree = new Tree({
                                    "title": bridgeFiles.title
                                });

                                bridge_files_tree.ui.titleDiv.on("click", function () {
                                    bridgeFiles.openAttachment();
                                });

                                tree.add(bridge_files_tree);


                            } else {

                                //目录
                                var childTree = new Tree({
                                    "title": item.name
                                });

                                childTree.ui.icon.addClass("list");

                                childTree.path = tree.path + item.name + "/";
                                childTree.ui.titleDiv.on("click", function () {
                                    _this.setFileTree(childTree);
                                });

                                tree.add(childTree);
                            }

                        });

                        tree.openChild();

                    });
                }

            }
        },
        "data"              : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"        : {
            value: function (list) {

                var _this = this;

                _this.bridges = {};

                $.each(list, function (i, item) {

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    if (_this.extendProperties && _this.extendProperties[item["编号"]] && _this.extendPropertiesMap) {
                        item.extendProperties = _this.extendPropertiesMap.format(_this.extendProperties[item["编号"]]);
                    }

                    item.app = _this.app;
                    var bridgeProfile = new BridgeProfile(item);

                    _this.items.push(bridgeProfile);

                    var bridgeFileTree = new Tree({
                        "title" : bridgeProfile.title,
                        "bridge": bridgeProfile,
                        "list"  : []
                    });

                    _this.filePanel.treeBlock.tree.bridgeTree[bridgeProfile.id] = _this.filePanel.treeBlock.tree.bridgeTree.add(bridgeFileTree);
                    _this.filePanel.treeBlock.tree.bridgeTree[bridgeProfile.id].isChildLoaded = false;

                    bridgeProfile.listItem = new ListItem({
                        "title"        : bridgeProfile["名称"],
                        "subtitle"     : bridgeProfile["类型"],
                        "icon"         : _this.app.icons.bridge.base64,
                        "bridgeProfile": bridgeProfile
                    });

                    bridgeProfile.on("select", function (bridgeProfile) {
                        _this.trigger("bridgeSelect", bridgeProfile);
                    });

                    bridgeProfile.on("elementSelect", function (elementNum) {
                        _this.trigger("bridgeElementSelect", elementNum);
                    });

                    bridgeProfile.listItem.on("click", function () {
                        _this.trigger("bridgeSelect", bridgeProfile);
                    });

                    _this.listPanel.listBlock.add(bridgeProfile.listItem);

                });


            }
        },
        "clear"             : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "open"              : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {
                    _this.isOpened = true;

                    _this.listPanel.open(false);

                    $.each(_this.items, function (i, item) {
                        item.open();
                    });

                    _this.filePanel.open(false);
                    _this.setFileTree(_this.filePanel.treeBlock.tree.root);

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit"          : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.load({}, function (data, mapData) {

                        _this.bridgePropertiesRequest.send({}, function (json) {

                            _this.extendProperties = json.data;

                            if (mapData) {
                                _this.objMap = mapData.obj;
                                _this.propertiesMap = mapData.properties;
                                _this.extendPropertiesMap = new mhjs.Map(mapData.extendProperties);
                            }

                            _this.data = data;
                            _this.isLoaded = true;
                            callback && callback();


                        });


                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel"         : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"             : {
            value: function () {
                var _this = this;
                _this.closePanel();
                $.each(_this.items, function (i, item) {
                    item.close();
                });
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "closePanel"        : {
            value: function () {
                var _this = this;
                _this.closeAllPanel();
                _this.isPanelOpened = false;
                _this.trigger("closePanel");
            }
        },
        "activeItem"        : {
            value: function (item) {
                var _this = this;

                if (!item.isActive) {
                    _this.unActiveAllItem();
                    item.active();
                    item.listItem.addClass("active");
                }

            }
        },
        "unActiveAllItem"   : {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        },
        "loadBridgeElements": {
            value: function (bridgeProfile, callback) {
                var _this = this;

                if (!bridgeProfile.elementsLoaded) {
                    _this.app.servers.DC.requests.bridge_node.send({"bridgeId": bridgeProfile.id}, function (json) {

                        bridgeProfile.elementsLoaded = true;
                        bridgeProfile.elements = json.data.list;
                        callback && callback(json.data.list);

                    });
                } else {
                    callback && callback(bridgeProfile.elements);
                }
            }
        },
        "renderElementsTree": {
            value: function (list) {

                var _this = this;
                var result = [];

                $.each(list, function (i, item) {
                    if (item.children && item.children.length > 0) {
                        result.push({
                            "title": item.title,
                            "list" : _this.renderElementsTree(item.children)
                        });
                    } else {

                        var tree = new Tree({
                            "title": item.title
                        });

                        if (!item.bridgeFilesNums) {
                            tree.addClass("disable");
                        }

                        tree.ui.titleDiv.on("click", function () {
                            _this.trigger("bridgeElementSelect", item.num);
                        });

                        result.push(tree);
                    }
                });

                return result;

            }
        },
        "openElementFiles"  : {
            value: function (elementNum, callback) {

                var _this = this;
                _this.app.servers.DC.requests.bridge_node.send({"num": elementNum}, function (bridge_node_json) {

                    var success = false;

                    if (bridge_node_json.data.list.length > 0) {

                        var element = bridge_node_json.data.list[0];
                        _this.elementDetailPanel.treeBlock.title = element.title;
                        _this.elementDetailPanel.treeBlock.tree.clear();

                        if (element.bridgeFilesList.length > 0) {

                            success = true;

                            $.each(element.bridgeFilesList, function (i, bridge_files) {

                                bridge_files.fileFormat = _this.app.servers.DC.fileFormat.bridgeFile;
                                bridge_files.modalityWindow = _this.app.modalityWindow;

                                var bridgeFiles = new BridgeFiles(bridge_files);

                                var bridge_files_tree = new Tree({
                                    "title": bridgeFiles.title,
                                    "class": "fileTree"
                                });

                                bridge_files_tree.ui.titleDiv.on("click", function () {
                                    bridgeFiles.openAttachment();
                                });

                                _this.elementDetailPanel.treeBlock.tree.add(bridge_files_tree);

                                // if (bridgeFiles.bridgeFileList.length > 0) {
                                //
                                //     $.each(bridgeFiles.bridgeFileList, function (j, bridge_file) {
                                //
                                //         var bridgeFile = new BridgeFile(bridge_file);
                                //
                                //         var bridge_file_tree = new Tree({
                                //             "title": bridgeFile.fileName, "class": "fileTree"
                                //         });
                                //
                                //         bridge_file_tree.ui.titleDiv.on("click", function () {
                                //             bridgeFiles.openAttachment();
                                //         });
                                //
                                //         bridge_files_tree.add(bridge_file_tree);
                                //     });
                                //
                                // }
                            });

                        }

                    }

                    callback && callback(success);

                });
            }
        }
    });

    return _class;

});